package org.my.stock.util;

import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.internal.IgnitionEx;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class TypeDataUpdate {
    /**
     * 获取股票类型的数据
     * 板块资金流
     * https://data.eastmoney.com/bkzj/gn.html
     * */
    public static void updateData() {
        String filePath = "/Users/chenfei/Documents/Java/my_database/my-stock/src/main/resources/股票类型";
        StringBuilder content = new StringBuilder();

        try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }

            String springCfgPath = "/Users/chenfei/Documents/Java/my_database/my-stock/src/main/resources/default-config.xml";
            Ignite ignite = IgnitionEx.start(springCfgPath);
            CacheConfiguration<?, ?> cacheCfg = new CacheConfiguration<>("public_meta");
            cacheCfg.setSqlSchema("PUBLIC");

            IgniteCache<?, ?> cache = ignite.getOrCreateCache(cacheCfg);

            List<LinkedTreeMap> lst = MyUtil.getGson().fromJson(content.toString(), new TypeToken<List<LinkedTreeMap>>() {}.getType());
            for (LinkedTreeMap m : lst) {
                System.out.println(m.get("f12").toString() + " " + m.get("f14").toString());
                update(cache, m.get("f12").toString(), m.get("f14").toString());
            }
        } catch (IOException | IgniteCheckedException e) {
            System.err.println("读取文件失败: " + e.getMessage());
        }
    }

    public static Boolean hasExist(IgniteCache<?, ?> cache, String code, String name) {
        String sql = "select count(id) from public.stock_type where code = '"+ code +"' and name = '"+ name +"'";
        SqlFieldsQuery fieldsQuery = new SqlFieldsQuery(sql);
        List<List<?>> lst = cache.query(fieldsQuery).getAll();
        if (lst.isEmpty())
            return false;
        return true;
    }

    public static Long getPkid(IgniteCache<?, ?> cache) {
        String sql = "select count(id) from public.stock_type";
        SqlFieldsQuery fieldsQuery = new SqlFieldsQuery(sql);
        List<List<?>> lst = cache.query(fieldsQuery).getAll();
        if (lst.isEmpty())
            return 0L;
        List<?> rs = lst.get(0);
        return (Long)rs.get(0);
    }

    public static void update(IgniteCache<?, ?> cache, String code, String name) {
        if (!hasExist(cache, code, name)) {
            Long id = getPkid(cache) + 1;
            String sql = "insert into public.stock_type (id, code, name) values ("+ id +", '"+ code +"', '"+ name +"')";
            SqlFieldsQuery fieldsQuery = new SqlFieldsQuery(sql);
            cache.query(fieldsQuery).getAll();
        }
    }
}
